﻿using System.Drawing;
using System.Windows.Forms;

namespace 售后客户管理.Forms
{
    enum HidenMenu
    {
        Hiden = 0x1,
        View = 0x2,
    }
    public partial class FmMain : Form
    {
        public FmMain()
        {
            InitializeComponent();
            //this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
        }
        protected override void OnHandleCreated(System.EventArgs e)//WorkingArea do not hiden the taskbar

        {
            base.OnHandleCreated(e);
            this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
        }
        private HidenMenu _hidenMenu = HidenMenu.View;
        public ModelLibrary.Customer Customer;



        //a status of plmenu which is use to hiden or view menus
        private HidenMenu GethidenMenu()
        { return _hidenMenu; }
        //a status of plmenu which is use to hiden or view menus
        private void SethidenMenu(HidenMenu value)
        {
            //if this status not changed,this do nothing
            if (_hidenMenu != value)
            {
                _hidenMenu = value;
            }
        }
        private void FmMain_Load(object sender, System.EventArgs e)//do something on form loading
        {
            this.plmain.BackColor = Common.MyColor.NewColor_LightLavender;
            this.panel1.BackColor = Common.MyColor.NewColor_Lavender;
            this.plButton.BackColor = Common.MyColor.NewColor_LightLightGray;
            //do something on form Closing
            this.FormClosing += Form1_FormClosing;
            //set the title to Center
            this.lbTitle.Left = (this.Size.Width - this.lbTitle.Width) / 2;
            //do something on form's size Changed
            this.Resize += FmMain_SizeChanged;
            this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
            OnHandleCreated(new System.EventArgs());
            this.btnHidenOrViewMenu.BackgroundImage = DrawingImage.UpImage(this.plHidenCtrl.Size);
            this.btnSelect.Paint += DrawingImage.RoundButton_Paint;
            this.btnCustomerInfo.Paint += DrawingImage.RoundButton_Paint;
            this.btnInternalSetting.Paint += DrawingImage.RoundButton_Paint;
            this.btnMaintenanceSelected.Paint += DrawingImage.RoundButton_Paint;
            this.btnHeNanMaintenance.Paint += DrawingImage.RoundButton_Paint;
            this.btnWorkPlan.Paint += DrawingImage.RoundButton_Paint;
            this.btnInternalSetting.Location = new Point(0, 0);
            this.plTitle.MouseDoubleClick += PlTitle_MouseDoubleClick;
            Btn_Click(this.btnSelect, new System.EventArgs());
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.DoubleClick += TabControl1_DoubleClick;
        }



        private void TabControl1_DoubleClick(object sender, System.EventArgs e)
        {
            TabPage tabPage = this.tabControl1.SelectedTab;
            this.tabControl1.Controls.Remove(tabPage);
            tabPage.Dispose();
            if (this.tabControl1.TabPages.Count == 0)
            {
                HidenOrMenu(HidenMenu.View);
            }
        }
        private void HidenOrMenu(HidenMenu hidenMenu)
        {
            //change status of plmenu
            SethidenMenu(hidenMenu);
            //change text of button
            //this.btnHidenOrViewMenu.Text = hidenMenu == HidenMenu.View ? "△" : "X";
            btnHidenOrViewMenu.BackgroundImage = hidenMenu == HidenMenu.View ? DrawingImage.UpImage(this.plHidenCtrl.Size) : DrawingImage.DownImage(this.plHidenCtrl.Size);
            //change height of plmenu
            this.plMenu.Height = hidenMenu == HidenMenu.View ? 146 : 46;
        }
        private void FmMain_SizeChanged(object sender, System.EventArgs e)//Title's location changes when the form size changed
        {
            this.lbTitle.Left = (this.Size.Width - this.lbTitle.Width) / 2;
        }
        private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)//closing caption
        {
            //get a variable to hold the users response
            DialogResult result;
            //set the variable to the messagebox results
            result = MessageBox.Show("请确认是否关闭窗口", "退出程序", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            //check the users response before deciding what to do
            if (result == System.Windows.Forms.DialogResult.OK) //user clicked ok
            {
                //close the application
                e.Cancel = false;
            }
            else //user clicked cancel
            {
                //keep the application open
                e.Cancel = true;
            }
        }

        private void BtnHidenOrViewMenu_Click(object sender, System.EventArgs e)//click hiden button
        {
            HidenMenu hidenMenu = GethidenMenu() == HidenMenu.View ? HidenMenu.Hiden : HidenMenu.View;
            HidenOrMenu(hidenMenu);
        }
        //function button clicked,情况查询/客户资料/字段选项/内部设置/售后维护查询/河南维护
        public void Btn_Click(object sender, System.EventArgs e)
        {
            string names = ((Button)sender).Text;
            Form pageForm = new Form();
            switch (names)
            {
                case "情况查询":
                    pageForm = new FmSelectionsMsg();
                    break;
                case "客户资料":
                    pageForm = new FmCustomerInfo();

                    break;
                case "内部设置":
                    pageForm = new FmInternalSetting();
                    break;
                case "售后维护查询":
                    pageForm = new FmSalesServiceSelections();
                    break;
                case "河南维护":
                    pageForm = new FmSalesService();
                    break;
                case "工作计划":
                    pageForm = new FmWorkPlanList();
                    break;
            }

            AddNewTabPage(names, pageForm);
            if (true)
            {
                HidenOrMenu(HidenMenu.Hiden);
            }
        }


        //Set the Form's windowState to Minimized
        private void PlTitle_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }
        //View the time on toolStatus
        private void Timer1_Tick(object sender, System.EventArgs e)
        {
            this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString(ModelLibrary.AppSet.TimeFormatString);
        }
        private void BtnUpDown_Click(object sender, System.EventArgs e)
        {
            BtnHidenOrViewMenu_Click(this.btnHidenOrViewMenu, new System.EventArgs());
        }



        //if has a TabPage that named PageName,then selected it,else create it
        private void AddNewTabPage(string PageName, Form PageForm)
        {
            PageForm.TopLevel = false;
            PageForm.BackColor = Common.MyColor.NewColor_LightLavender;
            PageForm.FormBorderStyle = FormBorderStyle.None;
            foreach (TabPage tabPage in this.tabControl1.Controls)
            {
                if (tabPage.Name == PageName)
                {
                    this.tabControl1.Controls.Remove(tabPage);
                    //return;
                }
            }
            TabPage tabPageNew = new TabPage(PageName)
            {
                BackColor = Common.MyColor.NewColor_LightLavender,
                Name = PageName,
                Margin = new Padding(0)
            };
            PageForm.Dock = DockStyle.Fill;
            tabPageNew.Controls.Add(PageForm);
            this.tabControl1.Controls.Add(tabPageNew);
            this.tabControl1.SelectedTab = tabPageNew;

            PageForm.Show();

            if (Customer != null && PageName == "客户资料")
            {
                ((FmCustomerInfo)PageForm).ObjectToApp(Customer);
            }
        }

        private void btnMaxNormal_Click(object sender, System.EventArgs e)
        {
            this.WindowState = this.WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized;
        }
    }
}
